package com.ptv.database;

import java.util.Locale;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.ptv.teachaclass.R;

public class AlumnosCursorAdapter extends SimpleCursorAdapter{

	private static final String CAMPO_TELEFONO = "telefono";
	private static final String CAMPO_LUNES = "Lunes";
	private static final String CAMPO_MARTES = "Martes";
	private static final String CAMPO_MIERCOLES = "Miercoles";
	private static final String CAMPO_JUEVES = "Jueves";
	private static final String CAMPO_VIERNES = "Viernes";
	private static final String CAMPO_SABADO = "Sabado";
	private static final String CAMPO_DOMINGO = "Domingo";
    private Context mContext;
    private final LayoutInflater mInflater;

	   
	public AlumnosCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		
        mInflater=LayoutInflater.from(context);
        mContext=context;
	}

    private void setViewTag(View view, Object tag) { 
         view.setTag(tag); 
         if (view instanceof ViewGroup) { 
             for (int i=0; i < ((ViewGroup) view).getChildCount(); i++) { 
                 setViewTag(((ViewGroup) view).getChildAt(i), tag); 
             }
         } 
     }
    
    private String capitalizeString(String param){
    	String p = param.substring(0, 1);
    	String r = param.substring(1, param.length());
    	return p.toUpperCase() + r;
    }
    
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    	super.bindView(view, context, cursor);

        setViewTag(view, cursor.getString(cursor.getColumnIndex(CAMPO_TELEFONO)));
        
        TextView mobileNo=(TextView)view.findViewById(R.id.filaInferiorId);
        
        String lunes = cursor.getString(cursor.getColumnIndex(CAMPO_LUNES.toLowerCase()));
        String martes = cursor.getString(cursor.getColumnIndex(CAMPO_MARTES.toLowerCase()));
        String miercoles = cursor.getString(cursor.getColumnIndex(CAMPO_MIERCOLES.toLowerCase()));
        String jueves = cursor.getString(cursor.getColumnIndex(CAMPO_JUEVES.toLowerCase()));
        String viernes = cursor.getString(cursor.getColumnIndex(CAMPO_VIERNES.toLowerCase()));
        String sabado = cursor.getString(cursor.getColumnIndex(CAMPO_SABADO.toLowerCase()));
        String domingo = cursor.getString(cursor.getColumnIndex(CAMPO_DOMINGO.toLowerCase()));
        
        String rdo = "";
        if ("1".equals(lunes)){
        	rdo = (Locale.getDefault().getLanguage().equals("en")?"Monday":CAMPO_LUNES) + ",";
        }
        if ("1".equals(martes)){
        	rdo = rdo + (Locale.getDefault().getLanguage().equals("en")?"Tuesday":CAMPO_MARTES) + ",";
        }   
        if ("1".equals(miercoles)){
        	rdo = rdo + (Locale.getDefault().getLanguage().equals("en")?"Wednesday":CAMPO_MIERCOLES) + ",";
        } 
        if ("1".equals(jueves)){
        	rdo = rdo + (Locale.getDefault().getLanguage().equals("en")?"Thursday":CAMPO_JUEVES) + ",";
        }   
        if ("1".equals(viernes)){
        	rdo = rdo + (Locale.getDefault().getLanguage().equals("en")?"Friday":CAMPO_VIERNES) + ",";
        }           
        if ("1".equals(sabado)){
        	rdo = rdo + (Locale.getDefault().getLanguage().equals("en")?"Saturday":CAMPO_SABADO) + ",";
        }   
        if ("1".equals(domingo)){
        	rdo = rdo + (Locale.getDefault().getLanguage().equals("en")?"Sunday":CAMPO_DOMINGO);
        }           
        if (rdo.endsWith(",")){
        	rdo = rdo.substring(0, rdo.length()-1);
        }

        mobileNo.setText(rdo);        	

    }
    
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
    	final View view = mInflater.inflate(R.layout.fila_alumno,parent,false);
        return view;
    }

}
